package com.gao.controller;

import com.gao.dao.DepartmentDao;
import com.gao.dao.EmployeeDao;
import com.gao.pojo.Department;
import com.gao.pojo.Employee;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.List;

@Controller
public class EmployeeController {
    @Autowired
    EmployeeDao employeeDao;
    @Autowired
    DepartmentDao departmentDao;
    @GetMapping("/emps")
    public String list(Model model){
        Collection<Employee> all = employeeDao.getAll();
        //将结果放在请求中
//        System.out.println(all);
        model.addAttribute("emps",all);
        return "emp/list";
    }
    @GetMapping("/emp")
    public String toAddEmp(Model model){
        Collection<Department> departments = departmentDao.getDepartment();
        model.addAttribute("departments",departments);
        return "emp/add";
    }
    @PostMapping("/emp")
    public String addEmp(Employee employee){
//        System.out.println(employee);
        employeeDao.save(employee);
        return "redirect:/emps";
    }
    @GetMapping("/updateEmp")
    public String toUpdateEmp(Integer id,Model model){
        System.out.println(id);
        Employee employeeById = employeeDao.getEmployeeById(id);
        model.addAttribute("emp",employeeById);
        Collection<Department> department = departmentDao.getDepartment();
        model.addAttribute("departments",department);
        System.out.println(employeeById);
        System.out.println(department);
        return "emp/update";
    }
    @PostMapping("/saveUpdateEmp")
    public String updateEmp(Employee employee){
        employeeDao.save(employee);
        System.out.println("修改了"+employee);
        return "redirect:/emps";
    }
    @GetMapping("/delEmp/{id}")
    public String delEmp(@PathVariable("id") Integer id){
        employeeDao.delete(id);
        return "redirect:/emps";
    }
    @GetMapping("/user/loginout")
    public String userLoginOut(HttpSession httpSession){
        httpSession.invalidate();
        return "redirect:/index.html";
    }
}
